<?php

namespace app\api\controller;

use app\common\model\tang\Notice as NoticeModel;
use app\common\model\tang\NoticeApply;
use app\common\model\tang\NoticeLog;
use think\facade\Db;

class Notice extends Base
{
    /**
     * 获取最新发布的公告
     */
    public function index()
    {
        $type = $this->request->param('type');
        $mobile = $this->request->param('mobile');
        if (empty($type) || empty($mobile)) {
            return SendError('参数错误');
        }
        $where = [];
        $where ['is_show']= 1;
        $where ['is_apply']=0;
        $noticeInfo = NoticeModel::order('id desc')->field('title')->where($where)->whereFindInSet('merchant_list',$this->merchant_id)->findOrEmpty();
        return SendSuccess('success', $noticeInfo);
    }

    /**
     * 已发布的列表
     */
    public function NoticeList()
    {
        $type = $this->request->param('type');
        if(empty($type)){
            return  SendError('类型必传');
        }
        $where = [];
        if ($type==1){  //说明需要报名
            $where ['is_show']= 1;
            $where ['is_apply']=1;
        }else{
            $where ['is_show']= 1;
            $where ['is_apply']=0;
        }
        $noticeList = NoticeModel::where($where)->whereFindInSet('merchant_list',$this->merchant_id)->order('id desc')->select();
        foreach ($noticeList as $key => $val) {
            $noticeList[$key]['send_time'] = date('Y-m-d H:i:s', $val['create_time']);
            $noticeList[$key]['image'] = 'https://zdwy.kelimx.com'.$val['image'];
        }
        return SendSuccess('success', $noticeList);
    }

    /**
     * 详情
     */
    public function NoticeDetail()
    {
        $id = $this->request->param('id');
        $mobile = $this->request->param('mobile');
        if (empty($id) || empty($mobile)) {
            return SendError('参数错误');
        }
        $noticeDetail = NoticeModel::find($id);
        $isRead = NoticeLog::where(['noticeId' => $id, 'mobile' => $this->mobile])->find();

        $noticeDetail['content'] = html_entity_decode(str_replace('/uploads',$this->request->domain().'/uploads',$noticeDetail['content']), ENT_QUOTES);
        if ($isRead) {
            $noticeDetail['isRead'] = true;
        } else {
            $noticeDetail['isRead'] = false;
        }
        if($noticeDetail['is_apply']==1){
            $isApply = NoticeApply::where(['noticeId' => $id, 'mobile' => $this->mobile])->find();
            if ($isApply) {
                $noticeDetail['isApply'] = true;
            } else {
                $noticeDetail['isApply'] = false;
            }
        }else{
            $noticeDetail['isApply'] = false;
        }

        return SendSuccess('success', $noticeDetail);
    }
    /**
     * 报名
     */
    public function noticeApply(){
        $param = $this->request->param();
        Db::startTrans();
        try {
            $model = new NoticeApply();
            $isRead = $model->where(['noticeId' => $param['id'], 'userId' => $this->merchant_id])->find();
            $data['mobile'] =  $this->mobile;
            $data['userId'] = $this->merchant_id;
            $data['noticeId'] = $param['id'];
            if (!empty($isRead)) {
                return SendError('您已报名');
            }
            (new NoticeModel())->where('id',$param['id'])->inc('read_sum',1)->update();
            (new NoticeModel())->where('id',$param['id'])->dec('noread_sum',1)->update();
            $result = $model->save($data);
            if ($result) {
                Db::commit();
                return SendSuccess('报名成功', []);
            } else {
                return SendError('系统繁忙');
            }

        } catch (\Exception $exception) {
            Db::rollback();
            return SendError($exception->getMessage());
        }

    }
    /**
     * 用户点击已读
     */
    public function confirmRead()
    {
        $param = $this->request->param();
        Db::startTrans();
        try {
            $model = new NoticeLog();
            $isRead = $model->where(['noticeId' => $param['id'], 'userId' => $this->merchant_id])->find();
            $data['mobile'] =  $this->mobile;
            $data['userId'] = $this->merchant_id;
            $data['type'] = 1;
            $data['noticeId'] = $param['id'];
            if (!empty($isRead)) {
                return SendError('您已阅读');
            }
            (new NoticeModel())->where('id',$param['id'])->inc('read_sum',1)->update();
            (new NoticeModel())->where('id',$param['id'])->dec('noread_sum',1)->update();
            $result = $model->save($data);
            if ($result) {
                Db::commit();
                return SendSuccess('已读成功', []);
            } else {
                return SendError('系统繁忙');
            }

        } catch (\Exception $exception) {
            Db::rollback();
            return SendError($exception->getMessage());
        }
    }
}